<template>
    <!-- 新增收件人 -->
    <el-dialog :title="isEdit?'编辑供应商信息':'新增供应商信息'" :visible.sync="visible" :close-on-click-modal="false" @close="onClose">
        <el-form :model="form" ref="form" label-width="100px" :rules="rules" size="medium">
            <!-- 新增表单项 -->
            <el-row type="flex" justify="center">
                <el-form-item label="供应商编号" prop="account">
                    <el-input v-model="form.account"></el-input>
                </el-form-item>
                <el-form-item label="供应商简称" prop="alias">
                    <el-input v-model="form.alias"></el-input>
                </el-form-item>

            </el-row>
            <el-row type="flex" justify="center">
                <el-form-item label="电子邮箱" prop="email">
                    <el-input v-model="form.email"></el-input>
                </el-form-item>
                <el-form-item label="集团/组织" prop="group">
                    <el-input v-model="form.group"></el-input>
                </el-form-item>

            </el-row>
            <el-row type="flex" justify="center">
                <el-form-item label="绑定系统用户" prop="sysUserId">
                    <el-select v-model="form.sysUserId" placeholder="请选择" filterable>
                        <el-option v-for="item in selectDatas1" :key="item.userId" :label="item.userName"
                            :value="String(item.userId)">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="负责业务员" prop="chargeUser">
                    <el-select v-model="form.chargeUser" placeholder="请选择" filterable>
                        <el-option v-for="item in selectDatas2" :key="item.userId" :label="item.userName"
                            :value="String(item.userId)">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-row>
            <el-row type="flex" justify="center">
                <el-form-item label="供应商状态" prop="isDeleted">
                    <el-switch v-model="form.isDeleted" :active-value="false" :inactive-value="true" active-text="启用"
                        inactive-text="停用" />
                </el-form-item>
            </el-row>
            <el-form-item size="large" style="width:100%;text-align: center;">
                <el-button type="primary" @click="onSubmit">确定</el-button>
                <el-button @click="onClose">关闭</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
import request from '@/utils/request'

export default {
    name: "addtionMailUserDialog",
    props: {
        dialogVisible: {
            type: Boolean,
            default: false
        }
        ,data:{
            type:Object,
            default:()=>({}) 
        }
    }
    , data() {
        return {
            form: {
                account: ''
                , alias: ''
                , email: ''
                , group: ''
                , sysUserId:''
                , isDeleted:false
                ,isSubmit:false
                , chargeUser:''
            }
            , rules: {
                account: [
                    { required: true, message: '客户/供应商名不能为空。', trigger: 'blur' }
                ]
                , email: [
                    { required: true, message: '电子邮箱地址不能为空。', trigger: 'blur' }
                    , { type: "email", message: "请输入有效的电子邮箱。", trigger: ["blur", "change"] }
                ]
            }
            , visible: this.dialogVisible
            ,selectDatas1:[]
            , selectDatas2: []
            ,isEdit:false
        }
    }
    , methods: {
        onClose() {            
            this.visible = false;
            this.$emit('close',this.isSubmit);
        }
        , onSubmit() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.isSubmit = true;
                    this.submitRequest().then(res => {
                        if (res.code === 200) {
                            this.$modal.msgSuccess(this.isEdit?'编辑成功。':'新增成功。');
                            this.onClose();
                        } else {
                            this.$modal.msgError(res.msg);
                        }
                    }).catch(err => {
                        console.log(err);
                    }).finally(() => {
                        this.toggleButtons();
                    })
                } else {
                    this.$modal.msgError("请输入必填项/有效值。");
                }

            });
        }
        , submitRequest() {
            return request({
                url: '/file-share/add-receiver'
                , method: 'post'
                , data: this.form
            })
        }
        ,getSelectUserList(){
            this.$modal.loading("正在加载数据...");
            request({
                url: '/system/user/list'
               , method: 'get' 
                , params: { pageNum: 1, pageSize:99999}
            }).then(res=>{
                this.selectDatas1 = [{ userId: '', userName: '--' }];
                this.selectDatas2 = [{ userId: '', userName: '--' }];
                if(res&&res.code===200){
                    var supplierList = this.getSupplierUidData();
                    for (let em of res.rows) {
                        if(supplierList.includes(em.userId)){
                            this.selectDatas1.push({ userId: em.userId, userName: `${em.userName} (${em.nickName})` });
                        }else{
                            this.selectDatas2.push({ userId: em.userId, userName: `${em.userName} (${em.nickName})` });
                        }
                    }
                }
            })
            .catch(err=>{
               console.log(err);
            })
           .finally(()=>{
               this.$modal.closeLoading();
           })
        }
        , toggleButtons() {
        }
        , onClear() {
            this.$refs.form.resetFields();
        }
        , getSupplierUidData() {
            var listStr = localStorage.getItem('supplier-list');
            // 判断listStr是否为空
            if (listStr && listStr.length !== 0) {
                // 将字符串转换为数组
                return JSON.parse(listStr).map(item => Number(item.sysUserId));
            }
            return [];
        }

    }
    , mounted() {
        this.isEdit = this.data&&this.data.id&&this.data.id.length>0;
        if(this.isEdit){
            this.form = { ...this.data }
            delete this.form.updateTime;
            delete this.form.updateUser;
        }
        this.getSelectUserList();
    }
}
</script>

<style scoped></style>